package com.xpoint.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xpoint.backend.common.BaseContext;
import com.xpoint.backend.common.R;
import com.xpoint.backend.entity.ShoppingCart;
import com.xpoint.backend.mapper.ShoppingCartMapper;
import com.xpoint.backend.service.ShoppingCartService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Override
    public R<ShoppingCart> addSoppingCart(ShoppingCart shoppingCart) {
//        添加到购物车，首先要明白添加进来的有可能是套餐，也有可能是菜品，所以要进行判断
        if (shoppingCart != null) {
//            将用户的id设置进对象里面
            Long userId = BaseContext.getCurrentId();
            shoppingCart.setUserId(userId);
//            判断是菜品还是套餐
            Long dishId = shoppingCart.getDishId();
//          先查询看有没有菜品，没有再添加
            LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
            if (dishId != null) {
                shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId, dishId);
            } else {
                shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
            }
            ShoppingCart one = getOne(shoppingCartLambdaQueryWrapper);
//            判断菜品是否存在，如果存在就+1.不存存在就默认保存1
            if (one != null) {
//                存在
                one.setNumber(one.getNumber() + 1);
                if (this.updateById(one)) {
                    return R.success(one);
                }
            } else {
//                不存在
                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());
                if (this.save(shoppingCart)) {
                    return R.success(shoppingCart);
                }
            }
        }


        return null;
    }

    @Override
    public R<List<ShoppingCart>> findList() {
//        获取当前用户的id
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, currentId);
        List<ShoppingCart> list = list(lambdaQueryWrapper);
        if (list != null) {
            return R.success(list);
        }
        return R.error("空");
    }

    @Override
//    清空购物车的菜单
    public R<String> removeShoppingCart() {
//        获取用户的id
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, currentId);
        if (this.remove(lambdaQueryWrapper)) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    @Override
//    菜单数量减一
    public R<ShoppingCart> subShopping(ShoppingCart shoppingCart) {

//            条件构造器
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//            判断是菜品还是套餐的查询操作
        Boolean isDish = shoppingCart.getDishId() != null;
        if (isDish) {
//                菜品操作
//                查询当前的菜品id==数据库的菜品id
            lambdaQueryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());

        } else {
//                套餐操作
//                查询当前的套餐id==数据库的套餐id
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
//        查询根据菜品或套餐id查询。
        ShoppingCart one = getOne(lambdaQueryWrapper);
//        创建更新数量的条件构造器
        LambdaUpdateWrapper<ShoppingCart> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
//        判断是菜品还是套餐
        if (isDish) {
//            设置当前数量-1
            lambdaUpdateWrapper
                    .eq(ShoppingCart::getDishId, shoppingCart.getDishId())
                    .set(ShoppingCart::getNumber, one.getNumber() - 1);
        } else {
//            设置当前数量-1
            lambdaUpdateWrapper
                    .eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId())
                    .set(ShoppingCart::getNumber, one.getNumber() - 1);
        }
//        更新操作
        update(lambdaUpdateWrapper);
//        再查询一下
        ShoppingCart one1 = getOne(lambdaQueryWrapper);
        return R.success(one1);
    }


}
